package com.cus.tom.transfer.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.cus.tom.transfer.App;
import com.cus.tom.transfer.databinding.ChooseItemBinding;
import com.cus.tom.transfer.frag.InfoFragment;
import com.cus.tom.transfer.utils.Utils;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

public class SendInfoAdapter {

    String[] ss = new String[]{"联系人", "短信", "通话记录"};
    LinearLayout linearLayout;
    List<ChooseItemBinding> list = new ArrayList<>();
    InfoFragment act;

    public SendInfoAdapter(LinearLayout linearLayout, InfoFragment act) {
        this.linearLayout = linearLayout;
        this.act = act;
        linearLayout.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(linearLayout.getContext());
        for (int i = 0; i < ss.length; i++) {
            ChooseItemBinding binding = ChooseItemBinding.inflate(inflater, linearLayout, true);
            list.add(binding);
            binding.title.setText(ss[i]);
            binding.num.setVisibility(View.INVISIBLE);
            binding.arrow.setVisibility(View.INVISIBLE);
            updateItem(i);
        }
    }

    public void notifyItemChanged(int index, long... param) {
        list.get(index).size.setText(Utils.formatSize(param[1]));
        updateItem(index);
        list.get(index).check.setOnClickListener(v -> {
            boolean bo = !v.isSelected();
            v.setSelected(bo);
            act.updateSelect(index, bo);
        });
    }

    public void selectAll(boolean check) {
        for (int i = 0; i < list.size(); i++) {
            ChooseItemBinding binding = list.get(i);
            boolean bo = check;
            if (bo) {
                if (i == 0 && !XXPermissions.isGranted(App.getApp(), Permission.READ_CONTACTS)) {
                    bo = false;
                } else if (i == 1 && !XXPermissions.isGranted(App.getApp(), Permission.READ_SMS)) {
                    bo = false;
                } else if (i == 2 && !XXPermissions.isGranted(App.getApp(), Permission.READ_CALL_LOG)) {
                    bo = false;
                }
            }
            binding.check.setSelected(bo);
            act.updateSelect(i, bo);
        }
    }

    private void updateItem(int i) {
        ChooseItemBinding binding = list.get(i);
        if (i == 0) {
            checkPermission(binding, Permission.READ_CONTACTS);
        } else if (i == 1) {
            checkPermission(binding, Permission.READ_SMS);
        } else if (i == 2) {
            checkPermission(binding, Permission.READ_CALL_LOG);
        }
        list.get(i).btn.setOnClickListener(v -> {
            act.askPermission(i);
        });
    }

    private void checkPermission(ChooseItemBinding binding, String permission) {
        if (XXPermissions.isGranted(linearLayout.getContext(), permission)) {
            binding.btn.setVisibility(View.INVISIBLE);
            String s = binding.size.getText().toString();
            if (TextUtils.isEmpty(s)) {
                binding.bar.setVisibility(View.VISIBLE);
            } else {
                binding.bar.setVisibility(View.GONE);
            }
        } else {
            binding.btn.setVisibility(View.VISIBLE);
            binding.bar.setVisibility(View.GONE);
        }
    }
}
